package com.joysuch.wwyt.core.service.impl;

import com.joysuch.wwyt.workflow.entity.WorkFlowProcess;
import com.joysuch.wwyt.workflow.repository.WorkFlowProcessDao;
import com.joysuch.wwyt.workflow.service.WorkFlowProcessService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

@Component
@Slf4j
public class ComponentConvertCacheFactory {
    @Autowired
    WorkFlowProcessService workFlowProcessService;
    @Autowired
    WorkFlowProcessDao workFlowProcessDao;

    @PostConstruct
    private void init() {
        List<WorkFlowProcess> allProcess = workFlowProcessDao.findAll();
        if (CollectionUtils.isNotEmpty(allProcess)){
            allProcess.stream().filter(e->e.getFormJson() != null)
                    .forEach(each -> {
                        try{
                            workFlowProcessService.saveComponentToCache(each.getId(),each.getFormJson(),each.getTenentId(),each.getOrgCode());
                        }catch (Exception e) {
                            log.error("cache component failed : {}", each);
                        }
                    });
        }
    }
}